package com.gjt.dlmall.product.service.impl;

import com.gjt.dlmall.product.entity.AttrEntity;
import com.gjt.dlmall.product.service.AttrService;
import com.gjt.dlmall.product.vo.AttrGroupWithAttrsVO;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjt.common.utils.PageUtils;
import com.gjt.common.utils.Query;

import com.gjt.dlmall.product.dao.AttrGroupDao;
import com.gjt.dlmall.product.entity.AttrGroupEntity;
import com.gjt.dlmall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取指定分类下得所有数据
     * @param params
     * @param categoryId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        if(categoryId==0){
            //没有点击任何分类，查询所有属性分组信息
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );
            return new PageUtils(page);
        }else{
            //查询指定分类下的所有属性分组信息
            String key =(String) params.get("key");
           QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
            if(categoryId!=0){
                //查询条件一 category_id = ?
                queryWrapper.eq("category_id",categoryId);
            }
            //查询套件二 and （id=key or name like %key%）
            if (!StringUtils.isEmpty(key)){
                queryWrapper.and(obj->{
                    obj.eq("id",key).or().like("name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    /**
     * 获取分类下的所有的分组&关联属性
     * @param categoryId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrByCategoryId(Long categoryId) {
        //1、查询出分类下的所有分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", categoryId));
        //2、查询所有的属性
        List<AttrGroupWithAttrsVO> collect = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrsVO attrsVO = new AttrGroupWithAttrsVO();
            //属性对拷
            BeanUtils.copyProperties(attrGroupEntity, attrsVO);
            //设置关联属性
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrsVO.getId());
            attrsVO.setAttrs(relationAttr);
            return attrsVO;
        }).collect(Collectors.toList());
        return collect;
    }

}